package com.meirengu.webview.service.impl;

import com.meirengu.common.RedisClient;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.DateUtils;
import com.meirengu.utils.NumberUtil;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.common.UserChannelEnum;
import com.meirengu.webview.dao.KwInterfaceConfigDao;
import com.meirengu.webview.model.*;
import com.meirengu.webview.service.KwFjsPushService;
import com.meirengu.webview.service.KwXbbFilterService;
import com.meirengu.webview.service.KwXbbPushService;
import com.meirengu.webview.service.UserInfoService;
import com.meirengu.webview.utils.UserAgentRandomUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * KwXbbFilter服务实现层
 *
 * @author Mao
 * @create Fri May 04 15:38:46 CST 2018
 */
@Service
public class KwXbbFilterServiceImpl extends BaseServiceImpl<KwXbbFilter> implements KwXbbFilterService {

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private KwInterfaceConfigDao kwInterfaceConfigDao;

    @Autowired
    private KwXbbPushService kwXbbPushService;

    @Autowired
    private KwFjsPushService kwFjsPushService;


    Logger logger = LoggerFactory.getLogger(KwXbbFilterServiceImpl.class);

    /**
     * 发送新旦接口
     *
     * @param userInfo
     * @param user
     */
    public boolean sendXbb(UserInfo userInfo, User user) throws Exception {

        boolean sendFlag = false;
        try {
            String xbbIol = redisClient.get("iol-xbb");
            if ("N".equals(xbbIol)) {
                Date brithdayDate = userInfo.getBirthday();
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                String idCard = userInfo.getIdCard();
                Integer housingType = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                Integer income = userInfo.getSalary();
                String ip = userInfo.getIp();
                String incomeStr = "10000以上";
                //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                if (income.intValue() == 1) {
                    incomeStr = "3000以下";
                } else if (income.intValue() == 2) {
                    incomeStr = "3000-5000";
                } else if (income.intValue() == 3) {
                    incomeStr = "5001-8000";
                } else if (income.intValue() == 4) {
                    incomeStr = "8001-10000";
                }
                String hasHouse = "有";
                //名下房产  1 无房   2 有房贷   3有房无贷
                if (housingType.intValue() == 1) {
                    hasHouse = "无";
                }
                String hasCar = "有";
                if (car.intValue() == 1) {
                    hasCar = " 无";
                }
                String isCommercialInsurance = "有";
                //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                if (lifeInsurance.intValue() == 1) {
                    isCommercialInsurance = " 无";
                }

                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwXbbFilter kwXbbFilter = new KwXbbFilter();
                kwXbbFilter.setUserId(id);
                kwXbbFilter.setMobile(mobile);
                kwXbbFilter.setChannelCode("xbb");
                kwXbbFilter.setChannelName("新贝");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("xbb");


                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwXbbFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwXbbFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwXbbFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        String sexStr = (sex == 0 ? "MALE" : "FEMALE");
                        String birthdayStr = DateUtils.dateToStr(brithdayDate, "yyyy-MM-dd");
                        Integer code = kwXbbPushService.applyInsurance(name, mobile, birthdayStr, sexStr, ip, expectAmount.toString(), incomeStr,
                                hasHouse, hasCar, isCommercialInsurance, user.getUserAgent(), idCard);
                        kwXbbFilter.setSuited("Y");
                        kwXbbFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, "xbb", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }

                insert(kwXbbFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }


    /**
     * 发送新旦平安险接口
     *
     * @param userInfo
     * @param user
     */
    @Override
    public boolean sendXbbForPingan(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {
            //校验是否有熠巨发送成功的记录
            Map<String, Object> yijuparams = new HashMap<>();
            yijuparams.put("mobile", user.getMobile());
            yijuparams.put("channelCode", "yiju");
            yijuparams.put("code", 1);
            Integer yijuPushCount = kwFjsPushService.getCount(yijuparams);
            //已有成功发送记录
            if (!NumberUtil.isNullOrZero(yijuPushCount)) {
                return sendFlag;
            }

            String xbb2Iol = redisClient.get("iol-xbb2");
            if ("N".equals(xbb2Iol)) {
                Date brithdayDate = userInfo.getBirthday();
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                String idCard = userInfo.getIdCard();
                Integer creditSituation = userInfo.getCredit();
                Integer housingType = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                expectAmount = expectAmount.add(new BigDecimal(50000));
                Integer profession = userInfo.getOccupation();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer housingFund = userInfo.getFund();
                Integer income = userInfo.getSalary();
                Integer paytype = userInfo.getSalaryMethod();
                String incomeStr = "10000以上";
                //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                if (income.intValue() == 1) {
                    incomeStr = "3000以下";
                } else if (income.intValue() == 2) {
                    incomeStr = "3000-5000";
                } else if (income.intValue() == 3) {
                    incomeStr = "5001-8000";
                } else if (income.intValue() == 4) {
                    incomeStr = "8001-10000";
                }
                String hasHouse = "有";
                //名下房产  1 无房   2 有房贷   3有房无贷
                if (housingType.intValue() == 1) {
                    hasHouse = "无";
                }
                String hasCar = "有";
                if (car.intValue() == 1) {
                    hasCar = " 无";
                }
                String isCommercialInsurance = "有";
                //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                if (lifeInsurance.intValue() == 1) {
                    isCommercialInsurance = " 无";
                }
                String ip = userInfo.getIp();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwXbbFilter kwXbbFilter = new KwXbbFilter();
                kwXbbFilter.setUserId(id);
                kwXbbFilter.setMobile(mobile);
                kwXbbFilter.setChannelCode("xbb2");
                kwXbbFilter.setChannelName("新贝2");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("xbb2");


                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwXbbFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwXbbFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwXbbFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        String sexStr = (sex == 0 ? "MALE" : "FEMALE");
                        String birthdayStr = DateUtils.dateToStr(brithdayDate, "yyyy-MM-dd");
                        Integer code = kwXbbPushService.applyInsurance2(name, mobile, birthdayStr, sexStr, ip, expectAmount.toString(), incomeStr,
                                hasHouse, hasCar, isCommercialInsurance, user.getUserAgent(), idCard);
                        kwXbbFilter.setSuited("Y");
                        kwXbbFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, "xbb2", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }

                insert(kwXbbFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        /*畅愿失败投新旦*/
        if (!sendFlag) {
            this.sendXbb(userInfo, user);
        }
        return sendFlag;
    }

    /**
     * 发送新旦3接口
     *
     * @param userInfo
     * @param user
     */
    public boolean sendXbb3(UserInfo userInfo, User user) throws Exception {
        boolean sendFlag = false;
        try {
            String xbbIol3 = redisClient.get("iol-xbb3");
            if ("N".equals(xbbIol3)) {
                Date brithdayDate = userInfo.getBirthday();
                String id = user.getId();
                String name = user.getName();
                String mobile = user.getMobile();
                Integer sex = user.getSex();
                String city = userInfo.getScity();
                Integer creditSituation = userInfo.getCredit();
                Integer housingType = userInfo.getHouse();
                Integer car = userInfo.getCar();
                Integer lifeInsurance = userInfo.getPolicy();
                BigDecimal expectAmount = userInfo.getExpectAmount();
                String idCard = userInfo.getIdCard();
                Integer profession = userInfo.getOccupation();
                Integer socialSecurity = userInfo.getSocialSecurity();
                Integer housingFund = userInfo.getFund();
                Integer income = userInfo.getSalary();
                Integer paytype = userInfo.getSalaryMethod();
                String incomeStr = "10000以上";
                //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                if (income.intValue() == 1) {
                    incomeStr = "3000以下";
                } else if (income.intValue() == 2) {
                    incomeStr = "3000-5000";
                } else if (income.intValue() == 3) {
                    incomeStr = "5001-8000";
                } else if (income.intValue() == 4) {
                    incomeStr = "8001-10000";
                }
                String hasHouse = "有";
                //名下房产  1 无房   2 有房贷   3有房无贷
                if (housingType.intValue() == 1) {
                    hasHouse = "无";
                }
                String hasCar = "有";
                if (car.intValue() == 1) {
                    hasCar = " 无";
                }
                String isCommercialInsurance = "有";
                //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                if (lifeInsurance.intValue() == 1) {
                    isCommercialInsurance = " 无";
                }
                String fund = "有";
                //住房公积金   0无公积金   1一年以内    2一年以上
                if (housingFund == 0) {
                    fund = "无";
                }
                String ip = userInfo.getIp();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwXbbFilter kwXbbFilter = new KwXbbFilter();
                kwXbbFilter.setUserId(id);
                kwXbbFilter.setMobile(mobile);
                kwXbbFilter.setChannelCode("xbb3");
                kwXbbFilter.setChannelName("新贝3");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("xbb3");


                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwXbbFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwXbbFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwXbbFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        String sexStr = (sex == 0 ? "MALE" : "FEMALE");
                        String birthdayStr = DateUtils.dateToStr(brithdayDate, "yyyy-MM-dd");
                        Integer code = kwXbbPushService.applyInsurance3(name, mobile, birthdayStr, sexStr, ip, expectAmount.toString(), incomeStr,
                                fund, hasHouse, hasCar, isCommercialInsurance, user.getUserAgent(), idCard);
                        kwXbbFilter.setSuited("Y");
                        kwXbbFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        userInfoService.updateLimitNum(code, "xbb3", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }

                insert(kwXbbFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    @Override
    public boolean sendXbb3ByModel(LmUser lmUser) throws Exception {
        boolean sendFlag = false;
        try {
            String xbbIol3 = redisClient.get("iol-xbb3");
            if ("N".equals(xbbIol3)) {
                Date brithdayDate = lmUser.getBirthday();
                Integer id = lmUser.getId();
                String name = lmUser.getName();
                String mobile = lmUser.getPhone();
                Integer sex = lmUser.getSex();
                String city = lmUser.getAreaName();
                Integer creditSituation = lmUser.getCredit();
                Integer housingType = lmUser.getHouse();
                Integer car = lmUser.getCar();
                Integer lifeInsurance = lmUser.getPolicy();
                BigDecimal expectAmount = lmUser.getExpectAmount();
                String idCard = lmUser.getIdCard();
                Integer profession = lmUser.getOccupation();
                Integer socialSecurity = lmUser.getSocialSecurity();
                Integer housingFund = lmUser.getFund();
                Integer income = lmUser.getSalary();
                Integer paytype = lmUser.getSalaryMethod();
                String incomeStr = "10000以上";
                //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                if (income.intValue() == 1) {
                    incomeStr = "3000以下";
                } else if (income.intValue() == 2) {
                    incomeStr = "3000-5000";
                } else if (income.intValue() == 3) {
                    incomeStr = "5001-8000";
                } else if (income.intValue() == 4) {
                    incomeStr = "8001-10000";
                }
                String hasHouse = "有";
                //名下房产  1 无房   2 有房贷   3有房无贷
                if (housingType.intValue() == 1) {
                    hasHouse = "无";
                }
                String hasCar = "有";
                if (car.intValue() == 1) {
                    hasCar = " 无";
                }
                String isCommercialInsurance = "有";
                //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                if (lifeInsurance.intValue() == 1) {
                    isCommercialInsurance = " 无";
                }
                String fund = "有";
                //住房公积金   0无公积金   1一年以内    2一年以上
                if (housingFund == 0) {
                    fund = "无";
                }
                String ip = lmUser.getIp();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwXbbFilter kwXbbFilter = new KwXbbFilter();
                kwXbbFilter.setUserId(id.toString());
                kwXbbFilter.setMobile(mobile);
                kwXbbFilter.setChannelCode("xbb3");
                kwXbbFilter.setChannelName("新贝3");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("xbb3");


                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwXbbFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwXbbFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwXbbFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        String sexStr = (sex == 0 ? "MALE" : "FEMALE");
                        String birthdayStr = DateUtils.dateToStr(brithdayDate, "yyyy-MM-dd");
                        Integer code = kwXbbPushService.applyInsurance3(name, mobile, birthdayStr, sexStr, ip, expectAmount.toString(), incomeStr,
                                fund, hasHouse, hasCar, isCommercialInsurance, UserAgentRandomUtil.random(), idCard);
                        kwXbbFilter.setSuited("Y");
                        kwXbbFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        User user = new User();
                        user.setMobile(lmUser.getPhone());
                        user.setCreateDate(lmUser.getCreateDate());
                        userInfoService.updateLimitNum(code, "xbb3", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }

                insert(kwXbbFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }

    /**
     * 秒单数据发送新旦接口
     *
     * @param lmUser
     */
    public boolean sendMdzjXbb(LmUser lmUser) throws Exception {

        boolean sendFlag = false;
        try {
            String xbbIol = redisClient.get("iol-xbb");
            if ("N".equals(xbbIol)) {
                Date brithdayDate = lmUser.getBirthday();
                Integer id = lmUser.getId();
                String name = lmUser.getName();
                String mobile = lmUser.getPhone();
                Integer sex = lmUser.getSex();
                String city = lmUser.getAreaName();
                Integer creditSituation = lmUser.getCredit();
                Integer housingType = lmUser.getHouse();
                Integer car = lmUser.getCar();
                Integer lifeInsurance = lmUser.getPolicy();
                BigDecimal expectAmount = lmUser.getExpectAmount();
                String idCard = lmUser.getIdCard();
                Integer profession = lmUser.getOccupation();
                Integer socialSecurity = lmUser.getSocialSecurity();
                Integer housingFund = lmUser.getFund();
                Integer income = lmUser.getSalary();
                Integer paytype = lmUser.getSalaryMethod();
                String incomeStr = "10000以上";
                //月薪   1 3000以下  2 3000-5000  3 5001-8000   4 8001-10000  5 1w以上
                if (income.intValue() == 1) {
                    incomeStr = "3000以下";
                } else if (income.intValue() == 2) {
                    incomeStr = "3000-5000";
                } else if (income.intValue() == 3) {
                    incomeStr = "5001-8000";
                } else if (income.intValue() == 4) {
                    incomeStr = "8001-10000";
                }
                String hasHouse = "有";
                //名下房产  1 无房   2 有房贷   3有房无贷
                if (housingType.intValue() == 1) {
                    hasHouse = "无";
                }
                String hasCar = "有";
                if (car.intValue() == 1) {
                    hasCar = " 无";
                }
                String isCommercialInsurance = "有";
                //寿险  1 无寿险   2 寿险2400以下   3 寿险2400以上
                if (lifeInsurance.intValue() == 1) {
                    isCommercialInsurance = " 无";
                }
                String ip = lmUser.getIp();
                int age = 25;
                age = DateUtils.getAge(brithdayDate);

                KwXbbFilter kwXbbFilter = new KwXbbFilter();
                kwXbbFilter.setUserId(id.toString());
                kwXbbFilter.setMobile(mobile);
                kwXbbFilter.setChannelCode("xbb");
                kwXbbFilter.setChannelName("新贝");


                KwInterfaceConfig kwInterfaceConfig = kwInterfaceConfigDao.findByCode("xbb");


                boolean dataFlag = true;
                if (kwInterfaceConfig != null) {
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitArea())) {
                        city = city.replace("市", "");
                        String cityRange = kwInterfaceConfig.getAreaValue();
                        if (!cityRange.contains(city)) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AREA.getValue());
                            kwXbbFilter.setRefuseMsg("用户所在城市不满足：" + city);
                        }
                    }
                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAge())) {
                        if (age < kwInterfaceConfig.getMinAge().intValue() || age > kwInterfaceConfig.getMaxAge().intValue()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_AGE.getValue());
                            kwXbbFilter.setRefuseMsg("用户年龄不符合：" + age);
                        }
                    }

                    if (dataFlag && "Y".equals(kwInterfaceConfig.getLimitAmount())) {
                        if (expectAmount.intValue() < kwInterfaceConfig.getMinAmount() || expectAmount.intValue() > kwInterfaceConfig.getMaxAmount()) {
                            dataFlag = false;
                            kwXbbFilter.setSuited("N");
                            kwXbbFilter.setCode(UserChannelEnum.FAIL_LOW_AMOUNT.getValue());
                            kwXbbFilter.setRefuseMsg("申请贷款额度小于" + kwInterfaceConfig.getMinAmount() + "w：" + expectAmount);
                        }
                    }
                    if (dataFlag) {
                        String sexStr = (sex == 0 ? "MALE" : "FEMALE");
                        String birthdayStr = DateUtils.dateToStr(brithdayDate, "yyyy-MM-dd");
                        Integer code = kwXbbPushService.applyInsurance(name, mobile, birthdayStr, sexStr, ip, expectAmount.toString(), incomeStr,
                                hasHouse, hasCar, isCommercialInsurance, UserAgentRandomUtil.random(), idCard);
                        kwXbbFilter.setSuited("Y");
                        kwXbbFilter.setCode(UserChannelEnum.PASS.getValue());
                        /*推送接口后修改限制数量*/
                        User user = new User();
                        user.setMobile(lmUser.getPhone());
                        user.setCreateDate(lmUser.getCreateDate());
                        userInfoService.updateLimitNum(code, "xbb", user);
                        if (code.intValue() == Constants.BUSINESS_STATUS_SUCCESS) {
                            sendFlag = true;
                        }
                    }
                }

                insert(kwXbbFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        }
        return sendFlag;
    }
}
